/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 周斌
 * Date: 2024-06-28
 * Time: 9:29
 */
public class Main {
    //day17 贪心算法练习 力扣605 种花问题 （判断相邻三个是否全为0，注意首尾）
    public boolean canPlaceFlowers(int[] flowerbed, int n) {
        int m = flowerbed.length;
        int i = 0;
        while(i < m) {
            if(flowerbed[i] == 0 && (i == 0 || flowerbed[i - 1] == 0) && (i == m-1 || flowerbed[i + 1] == 0)) {
                n--;
                i++;
            }
            i++;
        }
        return n <= 0;
    }
    //day17 贪心算法练习 力扣680 验证回文串||
    private  boolean isPalindrome (String s,int i, int j) {
        while(i < j) {
            if (s.charAt(i++) != s.charAt(j--)) {
                return false;
            }
        }
        return true;

    }
    public boolean validPalindrome(String s) {
        int n = s.length();
        int left = 0;
        int right = n-1;
        while(left < right) {
            if(s.charAt(left) != s.charAt(right)) {
                //分情况 left+1 和right-1
            return isPalindrome(s,left,right-1) || isPalindrome(s,left+1,right);
            }
            left++;
            right--;
        }
       return true;
    }

    public static void main(String[] args) {
        Main main = new Main();
        int[] array1 = {1,0,0,0,1};
        int[] array2 = {1,0,0,1,0,0,1};
        int[] array3 = {1,0,0,0,0,1};
        System.out.println(main.canPlaceFlowers(array1, 1));
        System.out.println(main.canPlaceFlowers(array1, 2));
        System.out.println(main.canPlaceFlowers(array2, 1));
        System.out.println(main.canPlaceFlowers(array3, 2));
        String s = "deeee";
        String s1 = "abc";
        System.out.println(main.validPalindrome(s));
        System.out.println(main.validPalindrome(s1));
    }
}
